package com.yanqu.road.server.cmd.systemgroup.newmonopolymkt.resgrab;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.resgrab.NewMonopolyMarketBaseBattleObjectInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.CrossAppointCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketGroup;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;


@Cmd(code = Protocol.C_CROSS_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION, desc = "进入资源争夺战 抢夺/撤回位置")
public class CrossNewMonopolyMarketResGrabActionCmd extends CrossAppointCommand {

    private long userId ;
    private UserBaseInfo userBaseInfo;
    private NewMonopolyMarketProto.NewMonopolyMarketResGrabActionReqMsg ctsMsg;
    private List<NewMonopolyMarketBaseBattleObjectInfo> objectInfoList;
    // 鼓舞次数
    private int buffAddTimes ;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketResGrabActionCrossReqMsg req = NewMonopolyMarketProto.NewMonopolyMarketResGrabActionCrossReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        ctsMsg = req.getCtsMsg();
        if(req.hasPlayerInfo()){
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(req.getPlayerInfo());
        }
        if(ctsMsg.getType() == eMonopolyMarketResType.RES_KING_NEW.getValue()){
            objectInfoList = CrossNewMonopolyMarketGroup.formatPatronsList(req.getPatronsListList());
        }else if(ctsMsg.getType() == eMonopolyMarketResType.RES_QUEEN_NEW.getValue()){
            objectInfoList = CrossNewMonopolyMarketGroup.formatBeautyList(req.getBeautyListList());
        }
        buffAddTimes = req.getCtsMsg().getBuffAddTimes();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossNewMonopolyMarketGroup monopolyMarket = CrossNewMonopolyMarketMgr.getMarket(groupId);
        if(monopolyMarket == null){
            return;
        }

        if(userBaseInfo != null){
            CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        }

        MessageHelper.sendPacket(serverId, userId , YanQuMessageUtils.buildMessage(GameProtocol.S_NEW_MONOPOLY_MARKET_RES_GRAB_ACTION_FROM_CROSS,
                monopolyMarket.getMarketResRob().action(userId, serverId, ctsMsg, userBaseInfo, objectInfoList, buffAddTimes)));
    }

    @Override
    public void sendData() {

    }
}
